//
//  YJMenuView.m
//  CRJMenuProject
//
//  Created by crj on 2017/3/14.
//  Copyright © 2017年 crj. All rights reserved.
//

#import "YJMenuView.h"

#define kSelectViewTag 1000



#define kTitleColorNormal HEXCOLOR(kColorGray3, 1)
#define kTitleColorSelected HEXCOLOR(kColorBlue1, 1)
#define kSelectedViewColor HEXCOLOR(kColorBlue1, 1)
#define kSelectedViewHeight 3.0f

@interface YJMenuView()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, strong) UIView *selectView;

@end

@implementation YJMenuView

static float itemWidth = 0;

//- (id)initWithTitles:(NSArray *)titles
//{
//    self = [super init];
//    if (self) {
//        _titles = titles;
//        _btnArray = [[NSMutableArray alloc] init];
//        
//        [self makeUI];
//        
//    }
//    return self;
//}

- (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        _titles = titles;
        _btnArray = [[NSMutableArray alloc] init];
        itemWidth = frame.size.width/_titles.count;

        [self makeUI];
    }
    return self;
}

- (void)makeUI{
    
    [_btnArray removeAllObjects];

    self.backgroundColor = [UIColor whiteColor];
    CGFloat fontSize = 15;
    
    for(int i=0; i<_titles.count; i++){
        NSString *title = _titles[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:kTitleColorNormal
                     forState:UIControlStateNormal];
        [button setTitleColor:kTitleColorSelected
                     forState:UIControlStateSelected];
        [button addTarget:self
                   action:@selector(selectAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        [self addSubview:button];
        [_btnArray addObject:button];
        
    }
    
    [self addSubview:self.selectView];
    
}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    
    for(int i=0; i<_titles.count; i++){
        
        UIButton *button = _btnArray[i];
        button.frame = CGRectMake(itemWidth*i, 0, itemWidth, self.frame.size.height);
    }
    
    CGRect selectedFrame = _selectView.frame;
    selectedFrame.origin.y = self.frame.size.height-kSelectedViewHeight;
    _selectView.frame = selectedFrame;
}

- (void)selectAction:(UIButton *)btn{
    
    if(btn.selected){
        return;
    }
    
    self.currentIndex = btn.tag;
    
}

#pragma mark - SET

- (void)setCurrentIndex:(NSInteger)currentIndex{

    _currentIndex = currentIndex;
    
    for(UIButton *button in _btnArray){
        
        button.selected = NO;
    }
    UIButton *selectedBtn = _btnArray[_currentIndex];
    selectedBtn.selected = YES;
    
    CGFloat selectTitleWidth = [self widthOfString:selectedBtn.titleLabel.text font:selectedBtn.titleLabel.font height:selectedBtn.titleLabel.frame.size.height];
    CGFloat endX = itemWidth*_currentIndex+((itemWidth-selectTitleWidth)/2);
    [UIView animateWithDuration:0.3 animations:^{
        _selectView.frame = CGRectMake(endX, self.frame.size.height-kSelectedViewHeight, selectTitleWidth, kSelectedViewHeight);
    }];
    
    if([_delegate respondsToSelector:@selector(menuSelectWithIndex:)]){
        [_delegate menuSelectWithIndex:_currentIndex];
    }
}

#pragma mark - GET

- (UIView *)selectView{
    
    if(!_selectView){
        
        _selectView = [[UIView alloc] init];
        _selectView.backgroundColor = kSelectedViewColor;
        _selectView.frame = CGRectMake(0, self.frame.size.height-kSelectedViewHeight, 0, kSelectedViewHeight);
    }
    return _selectView;
}


#pragma mark - Private

- (CGFloat)widthOfString:(NSString *)string font:(UIFont *)font height:(CGFloat)height
{
    NSDictionary * dict=[NSDictionary dictionaryWithObject: font forKey:NSFontAttributeName];
    CGRect rect=[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.width;
}




@end
